$(function () {
    var id = location.href.split("?id=")[1];
    init(id);
});

function init(id) {
     $.ajax({
        type: "GET",
        url: urlcore + "/api/questionnaire/selectOne?id=" + id,
        dataType: "json",
        headers: {"Content-Type": "application/json;charset=UTF-8"},
        data: {},
        success: function (data) {//data是返回的hash,key之类的值，key是定义的文件名
            $("#title").val(data.data.questionnaire.title);
            $("#naireId").val(data.data.questionnaire.id);
            var html = "";
            $.each(data.data.quesList,function (i,v) {
                var lex1,lex2 = "";
                if(v.type == 1){
                    lex1 = "selected";
                    lex2 = "";
                }else{
                    lex1 = "";
                    lex2 = "selected";
                }


                var quHhtml = "<div class=\"list-group\">\n" +
                    "                            <a href=\"#\" class=\"list-group-item \">\n" +
                    "                                <h4 class=\"list-group-item-heading\">\n" +
                    "                                    <div class=\"input-group \">\n" +
                    "                                        <span class=\"input-group-addon\">问题</span>\n" +
                    "                                        <input value=\""+v.title+"\" id=\"qc"+v.id+"\" type=\"text\" class=\"form-control\">\n" +
                    "                                    </div>\n" +
                    "                                    <div class=\"input-group \">\n" +
                    "                                        <span class=\"input-group-addon\">类型</span>\n" +
                    "                                        <select id=\"qt"+v.id+"\" class=\"selectpicker form-control\">\n" +
                    "                                        <option value=\"1\" "+lex1+" >单选</option>\n" +
                    "                                        <option value=\"2\"  "+lex2+">多选</option>\n" +
                    "                                        </select>\n" +
                    "                                    </div>\n" +
                    "                                    <div class=\"input-group-btn\" style=\"margin-top: 10px;padding-top: 10px\">\n" +
                    "                                        <button class=\" btn btn-info\" onclick=\"questEdit("+v.id+")\">修改</button>\n" +
                    "                                        <button class=\" btn btn-danger \" onclick=\"questDel("+v.id+")\">删除</button>\n" +
                    "                                    </div>\n" +
                    "                                </h4>\n" +
                    "                            </a>";
                quHhtml += "<a href=\"#\" class=\"list-group-item\">";
                var opHtml = "";
                    $.each(v.optionList,function (a,b) {
                        opHtml += "<h4 class=\"list-group-item-heading\">\n" +
                            "                                    <label class=\"form-label\">选项：</label><input id=\"op"+b.id+"\" value=\""+b.options+"\" type=\"text\" style=\"width:5%\" maxlength=\"1\">\n" +
                            "                                    <label class=\"form-label\">内容：</label><input id=\"oc"+b.id+"\" value=\""+b.content+"\" type=\"text\" style=\"width:50%\">\n" +
                            "                                    <button class=\"btn btn-info btn-xs\" onclick=\"optionEdit("+b.id+")\">修改</button>\n" +
                            "                                    <button class=\"btn btn-danger btn-xs\" onclick=\"optionDel("+b.id+")\">删除</button>\n" +
                            "                                </h4>";
                    });

                quHhtml += opHtml+ "</a><div>";
                html += quHhtml;
            });

        $("#divHtml").html(html);

        },
        error: function () {
            alert("查询失败");
        }
    });
}


//问卷标题修改
function naireEdit() {
    var id = $("#naireId").val();
    $.ajax({
        type: "POST",
        url: urlcore + "/api/questionnaire/update?id=" + id,
        dataType: "json",
        headers: {"Content-Type": "application/json;charset=UTF-8"},
        data: JSON.stringify({
            id:id,
            title:$("#title").val(),
        }),
        success: function (data) {
            if (data.success == true) {
                alert("修改成功");
            } else {
                alert("数据异常");
            }
        },
        error: function () {
            alert("失败");
        }
    });
}
//问题修改
function questEdit(id) {
    $.ajax({
        type: "POST",
        url: urlcore + "/api/question/update?id=" + id,
        dataType: "json",
        headers: {"Content-Type": "application/json;charset=UTF-8"},
        data: JSON.stringify({
            id:id,
            title:$("#qc"+id+"").val(),
            type:$("#qt"+id+"").val()
        }),
        success: function (data) {
            if (data.success == true) {
                alert("修改成功");
                location.reload();
            } else {
                alert("数据异常");
            }
        },
        error: function () {
            alert("失败");
        }
    });
}
//问题删除
function questDel(id) {
    if(confirm("确定删除吗？")) {
        $.ajax({
            type: "GET",
            url: urlcore + "/api/question/del?id=" + id,
            dataType: "json",
            headers: {"Content-Type": "application/json;charset=UTF-8"},
            data: {},
            success: function (data) {
                if (data.success == true) {
                    alert("删除成功");
                    location.reload();
                } else {
                    alert("数据异常");
                }
            },
            error: function () {
                alert("失败");
            }
        });
    }
}
//选项修改
function optionEdit(id) {
    $.ajax({
        type: "POST",
        url: urlcore + "/api/question/updateOption?id=" + id,
        dataType: "json",
        headers: {"Content-Type": "application/json;charset=UTF-8"},
        data: JSON.stringify({
            id:id,
            options:$("#op"+id+"").val(),
            content:$("#oc"+id+"").val()
        }),
        success: function (data) {
            if (data.success == true) {
                alert("修改成功");
                location.reload();
            } else {
                alert("数据异常");
            }
        },
        error: function () {
            alert("失败");
        }
    });
}
//选项删除
function optionDel(id) {
    if(confirm("确定删除吗？")) {
        $.ajax({
            type: "GET",
            url: urlcore + "/api/question/delOption?id=" + id,
            dataType: "json",
            headers: {"Content-Type": "application/json;charset=UTF-8"},
            data: {},
            success: function (data) {
                if (data.success == true) {
                    alert("删除成功");
                    location.reload();
                } else {
                    alert("数据异常");
                }
            },
            error: function () {
                alert("失败");
            }
        });
    }
}